<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  
  <style>
    
    .wrap {
      width: 400px;
      height: 400px;
      margin: 100px auto;
    }
    
    .wrap h1 {
      text-align: center;
    }
    
    .wrap div {
      width: 400px;
      height: 300px;
      background: pink;
      font-size: 30px;
      text-align: center;
      line-height: 300px;
    }
  
  </style>

</head>
<body>

<div class="wrap">
  <h1>按键改变颜色</h1>
  <div id="changBg">
    keyCode为：
    <span id="keyCodeSpan"></span>
  </div>
</div>

</body>
</html>

<script src="jq/jquery-1.12.2.js"></script>
<script>

  /**
   *
   * 在按键弹起的时候判断按下的是哪个按键，根据按键进行颜色的修改，如果没有这个按键的颜色，显示查无此键
   *
   */

  $(function () {   //入口函数
                    //按键抬起事件
    $(document).on("keyup", function (e) {
      //打印输出按键的阿斯特码
      console.log(e.keyCode);
      //获取按下的按键
      var code = e.keyCode;
      //按键抬起的时候显示当前按键的内容
      $("#keyCodeSpan").text(code);
      //使用 switch case 判断按下的按键来改变背景色
      switch (code) {
        case 66:
          $("#changBg").css("background-color", "blue");
          break;
        case 67:
          $("#changBg").css("background-color", "chocolate");
          break;
        case 71:
          $("#changBg").css("background-color", "green");
          break;
        case 75:
          $("#changBg").css("background-color", "khaki");
          break;
        case 76:
          $("#changBg").css("background-color", "lavender");
          break;
        case 79:
          $("#changBg").css("background-color", "orange");
          break;
        case 80:
          $("#changBg").css("background-color", "purple");
          break;
        case 82:
          $("#changBg").css("background-color", "red");
          break;
        case 87:
          $("#changBg").css("background-color", "white");
          break;
        case 89:
          $("#changBg").css("background-color", "yellow");
          break;
        default:
          $("#changBg").css("background-color", "pink");
          $("#keyCodeSpan").text("查无此键");
      }
    });
  });

</script>